/*
 * Created by Jaren at 2023/6/1 15:20
 */

import 'dart:typed_data';

import 'package:inmotion_protocol/common/exceptions.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/common/vehicle_info.dart';
import 'package:inmotion_protocol/lorin/factory/special/euc/v13_message_factory.dart';
import 'package:inmotion_protocol/lorin/lorin_message.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class V14MessageFactory extends V13MessageFactory {
  V14MessageFactory(ProductProperty productProperty) : super(productProperty);

  @override
  void initCompact(int apiVersion, VersionWrapper version) {
    compact = _V14CmdDefault();
  }

  LorinMessage genSetSpeedUnitMsg(int unit) {
    return genSpecialOne2OneWriteMsg(
        (compact! as _V14CmdDefault).speedUnitCmd(),
        ByteUtil.num2BytesLe(unit, 1));
  }

  LorinMessage genSetTwoBatteryModeMsg(bool on) {
    print("857--857: genSetTwoBatteryModeMsg = ${ByteUtil.bool2Bytes(on)}");
    return genSpecialOne2OneWriteMsg(
        (compact! as _V14CmdDefault).twoBatteryModeCmd(),
        ByteUtil.bool2Bytes(on));
  }

  @override
  LorinMessage genSetSpeedLimitMsg(int speedLimit, [int? speedLimit2]) {
    if (speedLimit2 == null)
      throw InvalidParamException("Param [speedLimit2] must not be null!");
    return genSpecialOne2OneWriteMsg(
        compact!.speedLimitCmd(),
        ByteUtil.buildBytes(ByteUtil.num2BytesLe(speedLimit, 2),
            ByteUtil.num2BytesLe(speedLimit2, 2)));
  }

  @override
  LorinMessage genPlaySoundItemMsg(int index) {
    return genSpecialOne2OneWriteMsg(
        compact!.playSoundItemCmd(), Uint8ClampedList.fromList([2, 100]));
  }
}

class _V14CmdDefault extends V13CmdDelegate {
  int speedUnitCmd() => 71;

  int twoBatteryModeCmd() => 72;

}
